package com.kotlin.myapplication.mvp.ui.start

import android.content.Intent
import android.text.TextUtils
import android.view.View
import android.widget.Toast
import com.kotlin.myapplication.R
import com.kotlin.myapplication.base.BaseActivity
import com.kotlin.myapplication.bean.UserBean
import com.kotlin.myapplication.bean.UserData
import com.kotlin.myapplication.mvp.model.UserContract
import com.kotlin.myapplication.mvp.presenter.UserPresenter
import com.kotlin.myapplication.mvp.ui.main.MainfirstActivity
import com.kotlin.myapplication.mananger.APPSPManager
import com.kotlin.myapplication.utils.Logger
import kotlinx.android.synthetic.main.activity_login.*

/**
 *@lil
 *@创建时间 2019/11/18 17:37
 *@描述
 */

class LoginActivity : BaseActivity(), View.OnClickListener, UserContract.view {


    override lateinit var presenter: UserContract.userModelResult


    override fun initView() {
        setContentView(R.layout.activity_login)

        presenter = UserPresenter(this)
        login.setOnClickListener(this)
        new_user.setOnClickListener(this)
    }


    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.new_user -> {
                APPSPManager.setleadPage(true)
                startActivity(Intent(this@LoginActivity, RegisterActivity::class.java))
                finish()
            }

            R.id.login -> {
                Logger.log("登录")
                if (TextUtils.isEmpty(input_account.getText().toString().trim())) {
                    Toast.makeText(this, "电话不能为空", Toast.LENGTH_SHORT).show()
                    return
                }
                if (TextUtils.isEmpty(input_password.getText().toString().trim())) {
                    Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show()
                    return
                }
                presenter.login(input_account.getText().toString().trim(), input_password.getText().toString().trim())
            }

        }
    }
    override fun onDestroy() {
        super.onDestroy()
        presenter.unsubscribe()
    }

    override fun showUserModel(user: UserBean<UserData>) {
        startActivity(Intent(this@LoginActivity, MainfirstActivity::class.java))
        finish()
    }

    override fun error(error: String) {
        showToast(error)
    }


}